package com.bjpowernode.task;

import com.bjpowernode.DlykServerApplication;
import com.bjpowernode.constant.DicEnum;
import com.bjpowernode.entity.TDicType;
import com.bjpowernode.entity.TProduct;
import com.bjpowernode.service.DicTypeService;
import com.bjpowernode.service.ProductService;
import jakarta.annotation.Resource;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 从数据库中读取数据字典放入缓存
 */
@EnableScheduling
@Component
public class DataTask {

    @Resource
    private DicTypeService dicTypeService;

    @Resource
    private ProductService productService;

    /**
     * 定时任务 3秒执行一次
     */
//    @Scheduled(cron = "0/3 * * * * ?")
    public void task() {
        //查询数据字典
        List<TDicType> dicTypeList = dicTypeService.getDicTypes();

        for(TDicType dicType : dicTypeList){
            /*
                key ： 性别  value : 男   女
                key : 称呼   value ： 先生  女士 夫人 博士 教授
             */
            DlykServerApplication.cacheMap.put(dicType.getTypeCode(), dicType.getDicValueList());
        }

        //查询意向产品
        List<TProduct> productList = productService.getProducts();
        DlykServerApplication.cacheMap.put(DicEnum.INTENTION_PRODUCT.getCode(), productList);
        System.out.println(DlykServerApplication.cacheMap);
    }
}
